package commentactivity;

import java.util.List;

import com.ronipl.createtabs.R;
import com.ronipl.createtabs.R.id;
import com.ronipl.createtabs.R.layout;
import com.ronipl.createtabs.R.menu;
import com.ronipl.formalisticsv2.model.valueobjects.IUserCommentActionListItem;
import com.ronipl.formalisticsv2.utilities.dummy.DummyCommentList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CommentActivity extends Activity {
	
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);
		
		list = (ListView)findViewById(R.id.comment_listview);
		list.setAdapter(new CommentbaseAdapter(this));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.comment, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
}

class CommentbaseAdapter extends BaseAdapter{
	
	List<IUserCommentActionListItem>list;
	
	Context context;
	
	public CommentbaseAdapter(CommentActivity commentActivity){
		
		context = (Context)commentActivity.getApplicationContext();
		list = DummyCommentList.getDummyCommentList();
		
	}
	
	

	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	//Create a holder
	class CommentViewHolder {
		
		TextView commentName;
		TextView commentDate;
		TextView commentMessage;
		
		public CommentViewHolder(View view){
			
			commentName = (TextView)view.findViewById(R.id.comment_name);
			commentDate = (TextView)view.findViewById(R.id.comment_date);
			commentMessage = (TextView)view.findViewById(R.id.comment_message);
			
		}
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View row = convertView;
		
		CommentViewHolder holder = null;
		
		if(row == null){
			
			LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.comment_view_list, parent, false);
			holder = new CommentViewHolder(row);
			row.setTag(holder);
		}
		else 
		{	
			holder = (CommentViewHolder)row.getTag();	
		}	
		
		IUserCommentActionListItem data = list.get(position);
		
		holder.commentName.setText(data.getCommentName());
		holder.commentDate.setText(data.getCommentDate());
		holder.commentMessage.setText(data.getCommentMessage());
		
		
		return row;
	}
	
}
